package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.constant.QiniuConstant;
import com.pro_one.constant.RedisConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Setmeal;
import com.pro_one.service.SetmealService;
import com.pro_one.util.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 套餐列表查询，以及根据条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByCondition")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findByCondition(queryPageBean);
    }
    @RequestMapping("/findByCondition2")
    public PageResult findByCondition2(@RequestBody QueryPageBean queryPageBean,Integer id){
        return setmealService.findByCondition1(queryPageBean,id);
    }
    /**
     * 上传图片
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            String name = multipartFile.getOriginalFilename();
            int index = name.lastIndexOf(".");
            String suffix = name.substring(index);
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, QiniuConstant.UPLOAD_URL + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 新增套餐以及关联的检查组信息
     *
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 根据id查询检查套餐
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 根据套餐id查询所有关联的检查组，并实现分页以及倒序显示
     *
     * @param queryPageBean
     * @param id
     * @return
     */
    @RequestMapping("/findAllGroupsByMealId")
    public PageResult findAllGroupsByMealId(@RequestBody QueryPageBean queryPageBean, Integer id) {
        PageResult pageResult = setmealService.findAllGroupsByMealId(queryPageBean, id);
        return pageResult;
    }

    /**
     * 根据id查询中间表关联的检查组id
     *
     * @param id
     * @return
     */
    @RequestMapping("/findAssociations")
    public Result findAssociations(Integer id) {
        try {
            List<Integer> associations = setmealService.findAssociations(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, associations);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    /*@RequestMapping("/findImgUrl")
    public Result findImgUrl(@RequestBody Setmeal setmeal) {
        try {
            String imgName = setmeal.getImg();
            return new Result(true, "获取图片成功", QiniuConstant.UPLOAD_URL + imgName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "呀！图片找不到了!");
        }
    }*/

    /**
     * 修改检查套餐信息，以及关联的检查组信息
     *
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS);
    }

    /**
     * 删除检查套餐以及对应的检查组
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteAllByMealId")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result deleteAllByMealId(Integer id) {
        try {
            setmealService.deleteAllByMealId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
    @RequestMapping("/findAllOptions3")
    public Result findAllOptions3(){
        List<Map<String,String>> list = setmealService.findAllOptions3();
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
    }
}
